package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.JFrame;

public class Insert extends JInternalFrame {
	private JLabel lblNewLabel;
	private JLabel lblEa;
	private JTextField codeTF;
	private JTextField eaTF;
	private JButton btn;
	private JLabel kekka;
	
	Connection con;
	DatabaseTest main;
	
	public Insert(Connection con,DatabaseTest main) {
		super("입력",true,true,true,true);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				codeTF.setText("");
				eaTF.setText("");
			}
		});
		this.con = con;
		this.main=main;
		setBounds(100, 100, 330, 250);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCodeTF());
		getContentPane().add(getEaTF());
		getContentPane().add(getBtn());
		getContentPane().add(getKekka());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(41, 36, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(41, 75, 57, 15);
		}
		return lblEa;
	}
	private JTextField getCodeTF() {
		if (codeTF == null) {
			codeTF = new JTextField();
			codeTF.setBounds(130, 33, 116, 21);
			codeTF.setColumns(10);
		}
		return codeTF;
	}
	private JTextField getEaTF() {
		if (eaTF == null) {
			eaTF = new JTextField();
			eaTF.setColumns(10);
			eaTF.setBounds(130, 72, 116, 21);
		}
		return eaTF;
	}
	private JButton getBtn() {
		if (btn == null) {
			btn = new JButton("저장");
			btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						String code = codeTF.getText();
						int ea = Integer.parseInt(eaTF.getText());
						
						String sql  = "insert into ptbl values(?,?,ptbl_serial.nextval)";
						
						PreparedStatement psmt= con.prepareStatement(sql);
						psmt.setString(1, code);
						psmt.setInt(2, ea);
						
						int rs = psmt.executeUpdate();
						if(rs==1){
							kekka.setText("코드명: "+code+ ", 수량: "+ea+"개가 입력되었습니다");
							codeTF.setText("");
							eaTF.setText("");
						}
						if(main.select != null && main.select.model != null){
							main.select.setList();
						}
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					
				}
			});
			btn.setBounds(130, 114, 97, 23);
		}
		return btn;
	}
	private JLabel getKekka() {
		if (kekka == null) {
			kekka = new JLabel("");
			kekka.setBounds(41, 159, 261, 21);
		}
		return kekka;
	}
	
}
